package cn.fxl.basic.service.impl;

import cn.fxl.basic.dto.SmsCodeDto;
import cn.fxl.basic.exception.BusinessExciption;
import cn.fxl.basic.service.IVerifyCodeService;
import cn.fxl.basic.utils.StrUtils;
import cn.fxl.basic.utils.VerifyCodeUtils;
import cn.fxl.user.domain.User;
import cn.fxl.user.mapper.UserMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.TimeUnit;


@Service
public class VerifyCodeServiceImpl implements IVerifyCodeService {
    //固定返回格式
    private  String str="data:image/jpeg;base64,";

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserMapper userMapper;

    /**
     * 获取手机注册图形验证码
     * @param key
     * @return
     */
    @Override
    public String getPhoneCode(String key) {
        String complexRandomString = StrUtils.getComplexRandomString(4);
        redisTemplate.opsForValue().set(key,complexRandomString,5, TimeUnit.MINUTES);
        String code = VerifyCodeUtils.verifyCode(115, 40, complexRandomString);
        return str+code;
    }

    @Override
    public void sendSmsCode(SmsCodeDto smsCodeDto) {
        //用户电话
        String phone = smsCodeDto.getPhone();
        //用户输入的图形验证码
        String imageCode = smsCodeDto.getImageCode();
        //图形验证码key
        String imageCodeKey = smsCodeDto.getImageCodeKey();
        if(StringUtils.isEmpty(phone)){
            throw new BusinessExciption("电话不能为空");
        }
        if(StringUtils.isEmpty(imageCode)){
            throw new BusinessExciption("请输入图形验证码");
        }
        User user=userMapper.getOneByPhone(phone);
        if(user!=null){
            throw new BusinessExciption("该账号已经注册");
        }
        Object msgValue = redisTemplate.opsForValue().get(imageCodeKey);
        if(msgValue==null){
            throw new BusinessExciption("图形验证码过期,请重新输入");
        }
        if(!msgValue.toString().equalsIgnoreCase(imageCode)){
            throw new BusinessExciption("图形验证码错误");
        }
        //1.校验 - 空值校验
//2.校验 - 图片验证码校验
        //2.1.图片验证码是否过期
        //2.2.图片验证码是否正确
//3.校验 - 手机号是否被注册 - 查询t_user表
//4.判断原来的验证码是否有效【3分钟过期 - redis】，从redis是否能拿到通过key【register:18708146200】
        //4.1.如果拿到了，表示有效,继续判断是否过了1分钟重发时间 value【9527:14688777777711】
        //4.1.1.如果没有过，就不应该再发，提示错误 - 请勿重发验证码
        //4.1.2.如果过了，就使用上次的验证码，但是要刷新过期时间【重新添加resis，用新的时间戳】
        //4.2.如果没有拿到，表示验证码过期了
        //4.2.1.重新生成验证码，并添加到redis
//5.调用短信接口发送短信
        Object SmsCodeKey = redisTemplate.opsForValue().get("register:" + phone);
       String SmsValueCode=null;
        if(SmsCodeKey!=null){
            long l = System.currentTimeMillis();
            String[] strings = SmsCodeKey.toString().split(":");
            String SmsCodeTime=strings[1];
            Long aLong = Long.valueOf(SmsCodeTime);
            if (l-aLong<=1*60*1000){
                throw new BusinessExciption("请勿频繁操作");
            }else {
                SmsValueCode=strings[0];
            }
        }else {
            String randomString = StrUtils.getRandomString(4);
            SmsValueCode=randomString;
        }
        redisTemplate.opsForValue().set("register:"+phone,SmsValueCode+":"+System.currentTimeMillis(),3,TimeUnit.MINUTES);
//                SmsUtil.sendSms(phone,"您的验证码为"+SmsValueCode+"请在3分钟内完成注册");
        System.out.println(phone+"您的验证码为"+SmsValueCode+"请在3分钟内完成注册");
    }

    /**
     * 微信绑定
     * @param map
     */
    @Override
    public void binderSmsCode(Map<String, String> map) {
        //用户电话
        String phone = map.get("phone");
        if(StringUtils.isEmpty(phone)){
            throw new BusinessExciption("电话不能为空");
        }
        Object SmsCodeKey = redisTemplate.opsForValue().get("binder:" + phone);
        String SmsValueCode=null;
        if(SmsCodeKey!=null){
            long l = System.currentTimeMillis();
            String[] strings = SmsCodeKey.toString().split(":");
            String SmsCodeTime=strings[1];
            Long aLong = Long.valueOf(SmsCodeTime);
            if (l-aLong<=1*60*1000){
                throw new BusinessExciption("请勿频繁操作");
            }else {
                SmsValueCode=strings[0];
            }
        }else {
            String randomString = StrUtils.getRandomString(4);
            SmsValueCode=randomString;
        }
        redisTemplate.opsForValue().set("binder:"+phone,SmsValueCode+":"+System.currentTimeMillis(),3,TimeUnit.MINUTES);
//                SmsUtil.sendSms(phone,"您的验证码为"+SmsValueCode+"请在3分钟内完成注册");
        System.out.println(phone+"您的验证码为"+SmsValueCode+"请在3分钟内完成注册");
    }
}
